using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using  server.status_db;
using EFCore.BulkExtensions;
using System.Dynamic;
using System.Linq.Dynamic.Core;

[Authorize]
public class YjjyqksController : ODataController
{
    private status_oneContext _db;
    readonly ILogger<YjjyqksController> _log;
    public YjjyqksController(status_oneContext context, ILogger<YjjyqksController> log)
    {
        _db = context;
        _log = log;
    }
    private bool YJJYQKExists(Guid key)
    {
        return _db.PG_B_YJJYQK.Any(p => p.ID == key);
    }
    [HttpPut]
    public async Task<IActionResult> Put([FromODataUri] Guid key, [FromBody]PG_B_YJJYQK update)
    {
        try
        {
            if (!ModelState.IsValid)
            {

                var message = string.Join(" | ", ModelState.Values
                               .SelectMany(v => v.Errors)
                               .Select(e => e.Exception.Message));
                _log.LogError(this.User.Identity.Name + "," + message);
                return StatusCode(500, message);
            }
            if (key != update.ID)
            {
                return StatusCode(500, "key not eq id");
            }
            _db.Entry(update).State = EntityState.Modified;
            try
            {
                await _db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException e)
            {

                var message = "多用户不能同时修改同一条数据";
                _log.LogError(this.User.Identity.Name + "," + message);
                return StatusCode(500, message);

            }
            return Updated(update);
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
    public async Task<IActionResult> Delete([FromODataUri] Guid key)
    {
        try
        {
            var mc = await _db.PG_B_YJJYQK.FindAsync(key);
            if (mc == null)
            {
                var message = "没有发现要删除的对象Id";
                 _log.LogError(this.User.Identity.Name + "," + message);
                return StatusCode(500,message);
            }
            _db.PG_B_YJJYQK.Remove(mc);
            await _db.SaveChangesAsync();
            return NoContent();
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
    [HttpPost]
    public async Task<IActionResult> Post([FromBody] PG_B_YJJYQK model)
    {
         try
        {
            if (!ModelState.IsValid)
            {
                var message = string.Join(" | ", ModelState.Values
                                .SelectMany(v => v.Errors)
                                .Select(e => e.Exception.Message));
                return StatusCode(500, message);
            }
            _db.PG_B_YJJYQK.Add(model);
            await _db.SaveChangesAsync();

            return Created(model);
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
    [EnableQuery]
    public IActionResult Get()
    {
       try
        {
            return Ok(_db.PG_B_YJJYQK);
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
    //
    [HttpPost]
    public IActionResult BulkAddYjjyqk(ODataActionParameters parameters)
    {
        
        var Yjjyqk = (IEnumerable<PG_B_YJJYQK>)parameters["Yjjyqk"];
        //
      
         _db.BulkInsert<PG_B_YJJYQK>( new List<PG_B_YJJYQK>(Yjjyqk));
        // Your data access layer logic goes here.
        return this.Ok(HttpStatusCode.NoContent);
    }
 [HttpGet("/api/Yjjyqks/RemoveAllData")]
   public IActionResult RemoveAllData(String GH,String XM,String XXID, String CJPC){
         _db.Database.ExecuteSqlCommand($"update PG_B_YJJYQK set 删除卡号={GH},删除姓名={XM},是否删除=1,删除时间= GETDATE() where XXID={XXID}  and  CJPC={CJPC}");
         return NoContent();
    }
        [HttpGet("/api/Yjjyqks/GetData")]
    public IActionResult GetData(String XXID, String CJPC,String query,string order,int limit,int skip,String viewMode)
    {
        //
        Console.WriteLine("query "+query);
        //
        
         var datas = _db.PG_B_YJJYQK.Where("XXID == @0 and CJPC == @1 and (是否删除 ==0 or 是否删除 == null)",XXID,CJPC);
         if(viewMode == "是")
         datas = datas.Where("审核状态 ==\"通过\"");
         if(query != null)
         datas = datas.Where(query);

         var Sums = new String[58];

        Sums[0]="合计";
         Sums[5]="系部数:"+datas.Select(e=>e.BMBH).Distinct().Count();
         Sums[7]="专业数:"+datas.Select(e=>e.ZYDM).Distinct().Count();
         Sums[9]="方向数:"+datas.Select(e=>e.ZYFXDM).Distinct().Count();
         Sums[10]="总人数:"+datas.Select(e=>e.BYSS).Sum();
         Sums[11]=""+datas.Select(e=>e.JYQK_0901_JYS).Sum();
         Sums[12]=""+((datas.Select(e=>e.JYQK_0901_JYS).Sum()/datas.Select(e=>e.BYSS).Sum())*100);
         Sums[13]=""+datas.Select(e=>e.JYQK_0901_BDS_JYS).Sum();
         Sums[14]=""+((datas.Select(e=>e.JYQK_0901_BDS_JYS).Sum()/datas.Select(e=>e.BYSS).Sum())*100);
         Sums[15]=""+datas.Select(e=>e.JYQK_0901_BSS_JYS).Sum();
         Sums[16]=""+((datas.Select(e=>e.JYQK_0901_BSS_JYS).Sum()/datas.Select(e=>e.BYSS).Sum())*100);
         Sums[17]=""+datas.Select(e=>e.JYQK_0901_BQY_JYS).Sum();
         Sums[18]=""+((datas.Select(e=>e.JYQK_0901_BQY_JYS).Sum()/datas.Select(e=>e.BYSS).Sum())*100);
         Sums[19]=""+datas.Select(e=>e.JYQK_0901_QT_JYS).Sum();
         Sums[20]=""+((datas.Select(e=>e.JYQK_0901_QT_JYS).Sum()/datas.Select(e=>e.BYSS).Sum())*100);
         Sums[21]=""+datas.Select(e=>e.JYQK_DKJY_JYS).Sum();
         Sums[22]=""+((datas.Select(e=>e.JYQK_DKJY_JYS).Sum()/datas.Select(e=>e.BYSS).Sum())*100);
         Sums[23]=""+datas.Select(e=>e.JYQK_PTGZ_BYS).Sum();
         Sums[24]=""+datas.Select(e=>e.JYQK_PTGZ_JYS).Sum();
         Sums[25]=""+((datas.Select(e=>e.JYQK_PTGZ_JYS).Sum()/datas.Select(e=>e.JYQK_PTGZ_BYS).Sum())*100);
         Sums[26]=""+datas.Select(e=>e.JYQK_SXS_BYS).Sum();
         Sums[27]=""+datas.Select(e=>e.JYQK_SXS_JYS).Sum();
         Sums[28]=""+((datas.Select(e=>e.JYQK_SXS_JYS).Sum()/datas.Select(e=>e.JYQK_SXS_BYS).Sum())*100);
         Sums[29]=""+datas.Select(e=>e.JYQK_WNGZ_BYS).Sum();
         Sums[30]=""+datas.Select(e=>e.JYQK_WNGZ_JYS).Sum();
         Sums[31]=""+((datas.Select(e=>e.JYQK_WNGZ_JYS).Sum()/datas.Select(e=>e.JYQK_WNGZ_BYS).Sum())*100);
         Sums[32]=""+datas.Select(e=>e.JYQK_QTBYS).Sum();
         Sums[33]=""+datas.Select(e=>e.JYQK_QTJYS).Sum();
         Sums[34]=""+((datas.Select(e=>e.JYQK_QTJYS).Sum()/datas.Select(e=>e.JYQK_QTBYS).Sum())*100);
         Sums[35]=""+datas.Select(e=>e.JYQK_QGTK_BYS).Sum();
         Sums[36]=""+datas.Select(e=>e.JYQK_QGTK_JYS).Sum();
         Sums[37]=""+((datas.Select(e=>e.JYQK_QGTK_JYS).Sum()/datas.Select(e=>e.JYQK_QGTK_BYS).Sum())*100);
         Sums[38]=""+datas.Select(e=>e.JYQK_SSTK_BYS).Sum();
         Sums[39]=""+datas.Select(e=>e.JYQK_SSTK_JYS).Sum();
         Sums[40]=""+((datas.Select(e=>e.JYQK_SSTK_JYS).Sum()/datas.Select(e=>e.JYQK_SSTK_BYS).Sum())*100);
         Sums[41]=""+datas.Select(e=>e.JYQK_ZZZS_BYS).Sum();
         Sums[42]=""+datas.Select(e=>e.JYQK_ZZZS_JYS).Sum();
         Sums[43]=""+((datas.Select(e=>e.JYQK_ZZZS_JYS).Sum()/datas.Select(e=>e.JYQK_ZZZS_BYS).Sum())*100);
         Sums[44]=""+datas.Select(e=>e.JYQK_ZSRX_BYS).Sum();
         Sums[45]=""+datas.Select(e=>e.JYQK_ZSRX_JYS).Sum();
         Sums[46]=""+((datas.Select(e=>e.JYQK_ZSRX_JYS).Sum()/datas.Select(e=>e.JYQK_ZSRX_BYS).Sum())*100);
         Sums[47]=""+datas.Select(e=>e.JYQK_DDZS_BYS).Sum();
         Sums[48]=""+datas.Select(e=>e.JYQK_DDZS_JYS).Sum();
         Sums[49]=""+((datas.Select(e=>e.JYQK_DDZS_JYS).Sum()/datas.Select(e=>e.JYQK_DDZS_BYS).Sum())*100);
         Sums[50]=""+datas.Select(e=>e.JYQK_ZZTY_BYS).Sum();
         Sums[51]=""+datas.Select(e=>e.JYQK_ZZTY_JYS).Sum();
         Sums[52]=""+((datas.Select(e=>e.JYQK_ZZTY_JYS).Sum()/datas.Select(e=>e.JYQK_ZZTY_BYS).Sum())*100);
         Sums[53]=""+datas.Select(e=>e.JYQK_QT_BYS).Sum();
         Sums[54]=""+datas.Select(e=>e.JYQK_QT_JYS).Sum();
         Sums[55]=""+((datas.Select(e=>e.JYQK_QT_JYS).Sum()/datas.Select(e=>e.JYQK_QT_BYS).Sum())*100);
         Sums[56]= "通过:"+datas.Where(e=>e.审核状态 == "通过").Count();
         Sums[56]+= ",不通过:"+datas.Where(e=>e.审核状态 == "不通过").Count();
         Sums[56] += ",待审核:"+datas.Where(e=>e.审核状态 == "待审核").Count();
 
        dynamic temp = new  ExpandoObject();
        temp.total = datas.Count();
        temp.values = datas.OrderBy(order).Skip(skip).Take(limit).ToArray();
          temp.sums = Sums;
        return this.Ok(temp);
    }
   
}